#version 300 es
precision mediump float;
uniform samplerCube sTexture;

in vec3 vEye;
in vec3 vNormal;

out vec4 fragColor;
vec4 zs( float f){

    vec3 vTextureCoord;
    vec4 finalColorZS;
    vec4 finalColorFS;
    vec4 finalColor ;
    const float maxH = 0.7 ;
    const float minH = 0.2 ;
    float sizeH = maxH-minH ;
    float testValue = abs(dot(vEye , vNormal)) ;
    if(testValue > maxH ){
        vTextureCoord =refract(-vEye,vNormal ,f);
        finalColor    =texture(sTexture, vTextureCoord);
    }else if(testValue < minH){
        vTextureCoord =reflect(-vEye,vNormal);
        finalColor    =texture(sTexture, vTextureCoord);
    }else{
        vTextureCoord =reflect(-vEye,vNormal);
        finalColorFS  =texture(sTexture, vTextureCoord);
        vTextureCoord =refract(-vEye,vNormal ,f);
        finalColorZS  =texture(sTexture, vTextureCoord);
        float ratio = (testValue - minH)/sizeH ;
        finalColor = finalColorZS*ratio + (1.0 - ratio)*finalColorFS ;
    }

    return finalColor ;
}

void main() {
    vec4 finalColor=vec4(0.0,0.0,0.0,0.0);
    finalColor.r=zs(0.97).r;
    finalColor.g=zs(0.955).g;
    finalColor.b=zs(0.94).b;
    fragColor=finalColor;
}
